package com.example.pingze.controller;

import com.example.pingze.entity.Comment;
import com.example.pingze.entity.Creation;
import com.example.pingze.entity.MyInfo;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.CommentService;
import com.example.pingze.service.CreationService;
import com.example.pingze.service.MyInfoService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/creation")
@CrossOrigin(origins = "*")
public class CreationController {
    @Autowired
    private CreationService service;
    @Autowired
    private MyInfoService myInfoService;

    @Autowired
    private CommentService commentService;

    /**
     * 添加创作
     * @param account
     * @param theme
     * @param title
     * @param content
     * @param time
     * @param nickname
     * @param imagePath 用户创作上传的多张图片
     * @return
     */
    @PostMapping("/addCreation")
    public Result<String> addCreation(@RequestParam String account,
                                      @RequestParam String theme,
                                      @RequestParam String title,
                                      @RequestParam String content,
                                      @RequestParam String time,
                                      @RequestParam String nickname,
                                      @RequestParam String imagePath) throws IOException {
        // 查找用户
        MyInfo myInfo = myInfoService.findMyInfoByAccount(account);
        if (myInfo == null) {
            throw new PingZeException("用户账号不存在");
        }
        Creation creation = new Creation();
        creation.setId(UUID.randomUUID().toString());
        creation.setAccount(account);
        creation.setTheme(theme);
        creation.setTitle(title);
        creation.setContent(content);
        creation.setTime(time);
        creation.setNickname(nickname);
        creation.setImagePaths(imagePath);
        service.addCreation(creation);
        return ResultVOUtils.success("success！");
    }

    @PostMapping("/getCreationList")
    public Result<List<Creation>> getCreationList(@RequestBody Creation creation) throws IOException {
        if(creation.getTheme() == null){
            throw new PingZeException("主题为空");
        }
        List<Creation> list = service.getCreationByTheme(creation.getTheme());
        return ResultVOUtils.result(list);
    }
    //最新排序
    //经常用
    @PostMapping("/getNewCreationList")
    public Result<List<Creation>> getNewCreationList(@RequestBody Creation creation) throws IOException {
        if(creation.getTheme() == null){
            throw new PingZeException("主题为空");
        }
        List<Creation> list = service.getNewCreationByTheme(creation.getTheme());
        return ResultVOUtils.result(list);
    }
    //最热排序
    @PostMapping("/getHottestCreationList")
    public Result<List<Creation>> getHottestCreationList(@RequestBody Creation creation) throws IOException {
        if(creation.getTheme() == null){
            throw new PingZeException("主题为空");
        }
        List<Creation> list = service.getHottestCreationByTheme(creation.getTheme());

        return ResultVOUtils.result(list);
    }
    //删除创作
    @PostMapping("/deleteCreation")
    public Result deleteCreation(@RequestParam String id,
                                 @RequestParam String account){
        Result result = new Result();
        if (id == null) {
            throw new PingZeException("id为空");
        }
        if (account == null) {
            throw new PingZeException("account为空");
        }
        Creation creationById = service.findCreationById(id);
        if (creationById == null) {
            throw new PingZeException("评论为null");
        }
        if (account.equals(creationById.getAccount())) {
            service.deleteCreation(creationById);
            result.setCode("150");
            result.setMsg("该条评论删除成功");
            return result;
        }
        return ResultVOUtils.success("创作删除成功");
    }
    //获取创作点赞数
    @PostMapping("/getLove")
    public Result getLove(String creationId){
        if (creationId == null) {
            throw new PingZeException("创作ID为空");
        }
        Creation creation = service.findCreationById(creationId);
        if (creation == null){
            throw new PingZeException("没有该条创作");
        }
        int love = creation.getLove();
        return ResultVOUtils.result(love);
    }
    //通过creationId获取该条创作的评论数
    @PostMapping("/getCounts")
    public Result getCounts(@RequestParam String creationId) {
        if (creationId == null) {
            throw new PingZeException("creation_未获取到creationId");
        }
        List<Comment> comments = commentService.findCommentsByCreationId(creationId);
        int count = comments.size();
        Creation creation = service.findCreationById(creationId);
        creation.setComment(count);
        service.saveCount(creation);
        System.out.println(count);
        return ResultVOUtils.result(count);
    }


    /**
     * 后台管理系统
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<Creation>> getList(
            Creation creation,
            @RequestParam(name = "pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize",required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order idOrder=Sort.Order.asc("id");
        Sort sort=Sort.by(idOrder);
        Pageable pageable= PageRequest.of(pageCode-1,pageSize,sort);
        Page page = service.getCreationList(creation,pageable);
        return ResultVOUtils.result(page);
    }

    @CrossOrigin
    @PostMapping("/add")
    public Result<Creation> add(String nickname, String time, String theme,
            String title, String content, String imagePaths, int love, int comment, String account
    ){
        Creation add = service.add(nickname, time, theme, title, content,
                imagePaths, love, comment, account);
        return ResultVOUtils.result(add);

    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<Creation> edit(String id, String nickname, String imageAvatar, String time, String theme, String title,
            String content, String imagePaths, int love, int comment, String account
    ){
        Creation edit = service.edit(
                id, nickname, imageAvatar, time, theme, title,
                content, love, comment, account);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @PostMapping("/delete")
    @Transactional
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        Creation creation = service.findCreationById(id);
        if(creation != null){
            service.deleteCreation(creation);
        }
        return ResultVOUtils.success("delete success!");
    }
}
